﻿package com.schimanek.animation  
{
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class PropertySwinger 
	{
		public var hasReachedGoal	:Boolean = false;
		
		
		private var _vel			:Number = 0;
		private var _acc			:Number = 0;
		private var _diff			:Number = 0;
		
		private var _oGoal			:Object;
		private var _oToSwing		:Object;
		private var _goalPropName	:String;
		private var _swingPropName	:String;
		private var _offset			:Number;
		
		
		public function PropertySwinger(oToSwing:Object, swingPropName:String, oGoal:Object, goalPropName:String,offset:Number=0) :void		
		{
			_oToSwing = oToSwing;
			_swingPropName = swingPropName;
			_oGoal = oGoal;
			_goalPropName = goalPropName;			
			_offset = offset;
		}
		public function swing(power:Number, inertia:Number):void
		{
			_diff =  _oToSwing[_swingPropName] - _oGoal[_goalPropName]-_offset;
			_acc = -power*_diff-_vel*inertia;
			_vel += _acc;
			_oToSwing[_swingPropName] += _vel;
			//trace(_oGoal);
			//CHECKING FINISH
			if (Math.abs(_diff) < Swinger.diffThreshold && Math.abs(_acc) < Swinger.accThreshold) {
				_oToSwing[_swingPropName] = _oGoal[_goalPropName] + _offset;
				hasReachedGoal = true;
			}else {
				hasReachedGoal = false;
			}
		}
	}
	
}